import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import Messaging.Drive;
import Messaging.IMessage;
import Messaging.Life;
import Messaging.Stamina;



	@SuppressWarnings("serial")
	class Key{
		private class Keys
		{
			public static final int UP = 0;
			public static final int DOWN = 1;
			public static final int RIGHT = 2;
			public static final int LEFT = 3;
			
					
		}
		
		protected Boolean pressed = false;
		ArrayBlockingQueue<IMessage> queue;
		public static ArrayList<Key> KeyList;
		Drive drive;
		
		public Key(ArrayBlockingQueue<IMessage> _queue)
		{
			queue = _queue;
			drive = new Drive();
		}

		public void Send()
		{
			
			if(KeyList.get(Keys.UP).GetPressed() && !KeyList.get(Keys.DOWN).GetPressed() && !KeyList.get(Keys.RIGHT).GetPressed() && !KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.Forward();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(!KeyList.get(Keys.UP).GetPressed() && KeyList.get(Keys.DOWN).GetPressed() && !KeyList.get(Keys.RIGHT).GetPressed() && !KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.Backward();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(!KeyList.get(Keys.UP).GetPressed() && !KeyList.get(Keys.DOWN).GetPressed() && KeyList.get(Keys.RIGHT).GetPressed() && !KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.Right();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else if(!KeyList.get(Keys.UP).GetPressed() && !KeyList.get(Keys.DOWN).GetPressed() && !KeyList.get(Keys.RIGHT).GetPressed() && KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.Left();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else if(KeyList.get(Keys.UP).GetPressed() && !KeyList.get(Keys.DOWN).GetPressed() && KeyList.get(Keys.RIGHT).GetPressed() && !KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.ForwardRight();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else if(KeyList.get(Keys.UP).GetPressed() && !KeyList.get(Keys.DOWN).GetPressed() && !KeyList.get(Keys.RIGHT).GetPressed() && KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.ForwardLeft();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else if(!KeyList.get(Keys.UP).GetPressed() && KeyList.get(Keys.DOWN).GetPressed() && KeyList.get(Keys.RIGHT).GetPressed() && !KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.BackwardRight();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else if(!KeyList.get(Keys.UP).GetPressed() && KeyList.get(Keys.DOWN).GetPressed() && !KeyList.get(Keys.RIGHT).GetPressed() && KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.BackwardLeft();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(!KeyList.get(Keys.UP).GetPressed() && !KeyList.get(Keys.DOWN).GetPressed() && !KeyList.get(Keys.RIGHT).GetPressed() && !KeyList.get(Keys.LEFT).GetPressed())
			{
				drive.Stop();
				try {
					queue.put(drive);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else
			{
				int u = 0;
			}
		}

		public void SetPressed(Boolean pressed)
		{
			this.pressed = pressed;
		}
		
		public Boolean GetPressed()
		{
			return pressed;
		}
	}
	
	
	class KeyUp extends AbstractAction 
	{
		Key key;
		public KeyUp(Key key)
		{
			this.key = key;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(key.GetPressed() != false)
			{
				key.SetPressed(false);
				key.Send();
			}
				
		}
		
	}
	
	class KeyDown extends AbstractAction 
	{
		Key key;
		public KeyDown(Key key)
		{
			this.key = key;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(key.GetPressed() != true)
			{
				key.SetPressed(true);
				key.Send();
			}			
		}
		
	}
	